const DB = require('../db')

async function save(site) {
    return await new DB.Site(site).save()
}

async function queryAll() {
    return await DB.Site.find({
        state: 1,
    }).select();
}

async function query(pageNum = 1, pageCount = 10, filter) {
    pageNum = Number(pageNum);
    pageCount = Number(pageCount);
    return await DB.Site
        .find(filter)
        .limit(pageCount)
        .skip((pageNum - 1) * pageCount)
        .select();
}

async function total() {
    return DB.Site.countDocuments({
        state: 1
    });
}

async function remove(id) {
    return DB.Site.updateOne({
        _id: id
    }, {
        state: 0
    })
}

async function update(site) {
    return DB.Site.updateOne({
        _id: site._id
    }, site)

}

module.exports = {
    save,
    queryAll,
    query,
    total,
    remove,
    update
}